package com.sso.managedBean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;

import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.component.widget.UIButton;
import org.operamasks.faces.event.EventBroadcaster;
import org.operamasks.faces.user.ajax.PartialUpdateManager;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.base.utils.IdUtils;
import com.sso.entity.DicHuman;
import com.sso.entity.DicSystem;
import com.sso.entity.DicSystemUser;
import com.sso.entity.DicUserHuman;
import com.sso.service.IDicHumanService;
import com.sso.service.IDicSystemService;
import com.sso.service.IDicSystemUserService;
import com.sso.service.IDicUserHumanService;

@ManagedBean(name = "sso_showUserBean", scope = ManagedBeanScope.REQUEST)
public class ShowUserBean {
	protected IDicSystemUserService dicSystemUserService = SpringBeanHolder
			.getBean("dicSystemUserService");

	private IDicSystemService dicSystemService = SpringBeanHolder
			.getBean("dicSystemService");

	protected IDicHumanService dicHumanService = SpringBeanHolder
			.getBean("dicHumanService");

	protected IDicUserHumanService dicUserHumanService = SpringBeanHolder
			.getBean("dicUserHumanService");

	@Inject
	private PartialUpdateManager update;

	@Accessible
	private boolean addButton = false;
	@Bind(id="add")
	private UIButton addButton_binding;
	
	@Bind
	@Accessible
	@SaveState
	private String id;
	
	@Bind
	@Accessible
	@SaveState
	protected String userID;
	
	@Bind
	@Accessible
	@SaveState
	protected DicSystemUser user;

	@Accessible
	@SaveState
	private String systemId;

	// 系统用户名称
	@Bind(id = "userName", attribute = "value")
	protected String userName;

	@Accessible
	@SaveState
	private DicUserHuman userHuman;

	@Inject
	private FacesContext context;

	@Inject
	private EventBroadcaster event;
	@SaveState
	private String first;

	@Bind(id = "listSystemForUserGrid", attribute = "binding")
	private UIDataGrid listSystemForUserGrid;

	@Bind(id = "listHumanForUserGrid")
	private UIDataGrid listHumanForUserGrid_binding;

	@BeforeRender
	public void beforeRender(boolean isPostBack) throws Exception {
		if (!isPostBack) {
			userID = context.getExternalContext().getRequestParameterMap().get(
					"id");
			if (!"".equals(userID) && null != userID) {
				user = dicSystemUserService.getByUserID(userID);
				if (user != null) {
					userID = user.getId();
					userName = user.getUserName();
				}
			}
		}
	}

	@Bind(id = "listSystemForUserGrid")
	private List<DicSystem> getSystemForUserGrid() {
		List<DicSystem> chList = new ArrayList<DicSystem>();
		chList = dicSystemService.getAllSystem();
		return chList;
	}

	@Action(id = "listSystemForUserGrid", event = "onrowselect")
	public void listSystemForUserGrid_onrowselect() {
		listHumanForUserGrid_binding.setSelections(null);
		update.markUpdate(listHumanForUserGrid_binding);
		listHumanForUserGrid_binding.reload();
		Object[] obj = listSystemForUserGrid.getSelectedValues();
		if (obj.length > 0) {
			DicSystem system = (DicSystem) obj[0];
			systemId = system.getId();
		}

		List<DicHuman> chList = new ArrayList<DicHuman>();
		chList = dicHumanService.getBySystemID(systemId);

		List<DicUserHuman> list = dicUserHumanService.getUserHumanBySystemId(
				userID, systemId);

		int[] selections;
		if (list.size() > 0) {
			userHuman = list.get(0);

			for (int i = 0; i < chList.size(); i++) {
				if (userHuman.getHuman().getId().equals(chList.get(i).getId())) {
					selections = new int[] { i };
					listHumanForUserGrid_binding.setSelections(selections);
					break;
				} else {
					listHumanForUserGrid_binding.setSelections(null);
				}
			}
		}else{
			userHuman = null;
		}
		update.markUpdate(listHumanForUserGrid_binding);
		listHumanForUserGrid_binding.reload();
	}

	@Bind(id = "listHumanForUserGrid")
	private List<DicHuman> getHumanForUserGrid() {
		List<DicHuman> chList = new ArrayList<DicHuman>();
		chList = dicHumanService.getBySystemID(systemId);
		if(chList.size()>0){
		addButton = true;
		addButton_binding.repaint();
		}
		return chList;
	}

	@Action(id = "add")
	public void addHumanUser() {
		Object[] obj = listHumanForUserGrid_binding.getSelectedValues();
		if (obj.length == 0) {
			Browser.alert("请选择用户列表中用户");
			return;
		} else {
			DicHuman hu = (DicHuman)obj[0];
			//首先判断userHuman是否存在
			//存在 则修改
			//无 则创建
			if(userHuman==null)
			{
				userHuman = new DicUserHuman();
				userHuman.setId(IdUtils.getUUID());
				userHuman.setUser(user);
				userHuman.setHuman(hu);
				dicUserHumanService.saveDicUserHuman(userHuman);
			}else if(userHuman!=null&&!hu.getId().equals(userHuman.getHuman().getId())){
				userHuman.setHuman(hu);
				dicUserHumanService.saveDicUserHuman(userHuman);
			}
			Browser.alert("系统用户保存成功！");
		}
	}

}
